package com.base.study.presenter;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.UpdateScopeStatEntity;
import com.base.commonlib.databinding.ItemDragVaildBinding;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.study.R;
import com.base.study.bean.QuestionType;
import com.base.study.bean.SubjectListBean;
import com.base.study.bean.TestPaperTypeBean;
import com.base.study.databinding.ItemTimedetailBinding;
import com.base.study.view.TestPaperTypeListView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class TestPaperTypeListPresenter extends BasePresenter<TestPaperTypeListView> {
    public TestPaperTypeListPresenter(TestPaperTypeListView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {
        super.start();
        startStudy();
    }

    /**
     * 获取大类
     * @param page
     * @param limit
     */
    public void getBigTypeList(final Integer page, Integer limit,String parentId,String tenantId){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("keyWord","");
        map.put("limit", limit);
        map.put("limitControlflg","1");
        map.put("page",page);
        map.put("tenantId",tenantId);
        map.put("ruleControlflg", "0");
        map.put("parentId", parentId);
        map.put("searchRuleId", "647bfb3d85a2455db772e4351af74145");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-searchcommon/searchMethod/useMethod").content(map)
                .build().enqueue(TestPaperTypeBean.class, new CallBackImpl<TestPaperTypeBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                        mView.returnError();
                    }

                    @Override
                    public void onNext(TestPaperTypeBean list) {
                        mView.hidePb();
                        if (page==1){
                            //刷新
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnBigTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnBigTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }

    /**
     * 积分
     */
    public void getJifeng(String courseRootId,String tenantId){
        Map<String, Object> map = new HashMap<>();
        Log.e("xxx",new Date()+"");
        if ("3aceee33bad7446698b5b493928c3c75".equals(tenantId)){
            map.put("settingKey","08020004");
        }
//        if ("95cf0f4698474b37a1c20af89b0b4217".equals(tenantId)){
//            map.put("settingKey","08020004");
//        }
        map.put("funcMainId", "edu");
        map.put("courseRootId", courseRootId);
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-function/integral/save").content(map)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                    }

                    @Override
                    public void onNext(String str) {
                        if (!str.contains("上限")){
                            mView.showMes(str);
                            RxBus2.getInstance().post(new UpdateScopeStatEntity(1));
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }



    /**
     * 获取小类
     * @param page
     * @param limit
     */
    public void getSmallTypeList(final Integer page, Integer limit,String parentId,String tenantId){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("keyWord","");
        map.put("limit", limit);
        map.put("tenantId",tenantId);
        map.put("limitControlflg","1");
        map.put("page",page);
        map.put("ruleControlflg", "0");
        map.put("parentId", parentId);
        map.put("searchRuleId", "647bfb3d85a2455db772e4351af74145");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-searchcommon/searchMethod/useMethod").content(map)
                .build().enqueue(TestPaperTypeBean.class, new CallBackImpl<TestPaperTypeBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                        mView.returnError();
                    }

                    @Override
                    public void onNext(TestPaperTypeBean list) {
                        mView.hidePb();
                        if (page==1){
                            //刷新
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnSmallTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnSmallTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }
    /**
     * 获取类型
     * @param page
     * @param limit
     */
    public void getType(final Integer page, Integer limit,String parentId){
        mView.showPb();
        Map<String, Object> map = new HashMap<>();
        map.put("keyWord","");
        map.put("limit", limit);
        map.put("limitControlflg","1");
        map.put("page",page);
        map.put("ruleControlflg", "0");
        map.put("parentId", parentId);
        map.put("searchRuleId", "6e8ad47e646a47ab82afcd3b673d13da");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-searchcommon/searchMethod/useMethod").content(map)
                .build().enqueue(QuestionType.class, new CallBackImpl<QuestionType>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showError();
                        mView.returnError();
                    }

                    @Override
                    public void onNext(QuestionType list) {
                        mView.hidePb();
                        if (page==1){
                            //刷新
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnQuestionTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnQuestionTypeList(list);
                            }else {
                                mView.returnError();
                            }
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }


    /**
     * 获取列表
     * @param page
     * @param limit
     * @param rootSubjectId
     * @param subjectId
     * @param typeId 题目类型id
     */
    public void getList(final Integer page, Integer limit,String rootSubjectId,String subjectId,String typeId){
        Map<String, Object> map = new HashMap<>();
        map.put("keyWord","");
        map.put("limit", limit);
        map.put("limitControlflg","1");
        map.put("page",page);
        map.put("ruleControlflg", "0");
        map.put("parentId", "0");
        map.put("rootSubjectId", rootSubjectId);
        map.put("subjectId", subjectId);
        map.put("typeId", typeId);
        map.put("searchRuleId", "0fa9a0e36ec249afa75870964bda49d1");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-searchcommon/searchMethod/useMethod").content(map)
                .build().enqueue(SubjectListBean.class, new CallBackImpl<SubjectListBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.showError();
                        mView.returnError();
                    }

                    @Override
                    public void onNext(SubjectListBean list) {
                        if (page==1){
                            //刷新
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnList(list);
                            }else {
                                mView.returnNoData();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.getRows() != null&&list.getRows().size()>0){
                                mView.returnMoreList(list);
                            }else {
                                mView.returnNoMoreData();
                            }
                        }
                    }
                });

        mCompositeDisposable.add(enqueue);
    }

    public void startStudy(){
        CommonDialog dialog = new CommonDialog(mActivity,"您要开始学习吗？","提示","取消","确定");
        dialog.setOnPosClick(new CommonDialog.OnPosClick() {
            @Override
            public void onClick() {
                mView.startStudy();
            }
        });
        dialog.show();
    }


    public void showTimeDetail(TextView tv){
        //设置contentView
        final ItemTimedetailBinding mBinding;
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.item_timedetail, null);
        mBinding = DataBindingUtil.bind(contentView);
        mBinding.tvDetail.setText("你当前已学习"+mView.returnStudyTiem());

        final PopupWindow popupWindow = new PopupWindow(contentView, BaseApp.SCREEN_WIDTH-150, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(contentView);
        //点窗口外关闭
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mActivity,R.color.color_white));
        popupWindow.setFocusable(true);
        //全局弹出
//        popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        popupWindow.setOutsideTouchable(true);
        //显示位置
        popupWindow.showAtLocation(tv, Gravity.CENTER,0,0);
        showHid(true,mActivity);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                showHid(false,mActivity);
            }
        });
    }
    public void showHid(boolean flag,Activity activity){
        if (flag) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.5f;
            activity.getWindow().setAttributes(lp);
        }else {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 1.0f;
            activity.getWindow().setAttributes(lp);
        }
    }
    //Util.java
    public String getHour(String date) {
        //秒数
//		Integer dt = Integer.parseInt(date) ;
//		if (dt < 3600) return Math.round(dt / 60) + "分钟";
//		int hour = Math.round(dt / 3600);
//		int minute = Math.round((dt - (hour * 3600)) / 60);
//		return hour + "小时" + (minute == 0 ? "" : minute + "分钟");

        //分钟
        Integer dt = Integer.parseInt(date) ;
        if (dt < 60) return dt + "秒";
        int hour = Math.round(dt / 60);
        int minute = Math.round(dt - (hour * 60));
        return hour + "分钟" + (minute == 0 ? "" : minute + "秒");
    }

}
